<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
use think\Validate;
use app\common\model\TimuRecord as TimuRecordModel;
use app\common\model\Subject;
use app\common\model\TimuZhishiRecord;
use app\common\model\TimuQuestions;
use app\common\model\TimuTalk;
use app\common\model\Zhishi;

/**
 * 错题接口
 */
class Timurecord extends Api
{
    protected $noNeedRight = '*';
    public function _initialize()
    {
        parent::_initialize();
        $this->model = new TimuRecordModel();
        $this->subjectModel = new Subject();
        $this->timuZhishiRecord = new TimuZhishiRecord();
        $this->timuQuestions = new TimuQuestions();
        $this->timuTalk = new TimuTalk();
        $this->zhishi = new Zhishi();

    }

    /**
     * 查询错题列表
     * @ApiTitle    (查询错题)
     * @ApiMethod  (GET)
     * @ApiParams   (name="student_id", type="int", required=true, description="学生ID")
     * @ApiParams   (name="correct", type="int", required=true, description="0：全部， 1：正确，2：错误")
     * @ApiParams   (name="subject_id", type="int", required=true, description="科目ID")
     * @ApiParams   (name="page", type="integer", required=false, description="页码")
     * @ApiParams   (name="limit", type="integer", required=false, description="每页数量")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="查询成功")
     * @ApiReturn   ({
           'code':'200',
           'msg':'查询成功',
           'time': "",
           'data': {
           }
      })
     */
    public function timurecordIndex()
    {

        $data = input('get.');
        $studentId = $data['student_id'];
        $correct = $data['correct'];
        $subjectId = $data['subject_id'];
        if (!$studentId || !Validate::is($studentId, 'number') || !$subjectId || !Validate::is($subjectId, 'number')){
            $this->error('参数错误');
        }

        $page = isset($data['page']) ? intval($data['page']) : 1;
        $limit = isset($data['limit']) ? intval($data['limit']) : 10;

        $subject = $this->subjectModel->dataInfo(['id' => $subjectId]);

        if ($subject) {
            $dis = [
                'student_id' => $studentId,
                'correct' => $correct,
                'subject' => $subject['subject']
            ];
            $row = $this->model->dataList($dis, 'id desc', $page, $limit);

        }else {
            $dis = [
                'student_id' => $studentId,
                'correct' => $correct,
            ];
            $row = $this->model->dataList($dis, 'id desc', $page, $limit);
        }

        $this->success('查询成功', $row);
    }

    /**
     * 查询错题详情
     * @ApiTitle    (查询错题详情)
     * @ApiMethod  (GET)
     * @ApiParams   (name="id", type="int", required=true, description="ID")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="查询成功")
     * @ApiReturn   ({
           'code':'200',
          'msg':'查询成功',
           'time': "",
           'data': {
           }
      })
     */
    public function timurecordDetail(){
        $data = input('get.');
        $id = $data['id'];
        if (!$id || !Validate::is($id, 'number')) {
            $this->error('参数错误');
        }

        $row = $this->model->dataInfo(['id' => $id]);

        $zhishiId = $this->timuZhishiRecord->dataMemberId(['timu_id' => $id], 'zhishi_id');

        $dis = [
            'id' => [ 'in' ,$zhishiId]
        ];
        $zhishiRecord = $this->zhishi->dataList($dis);
        $row['zhishi'] = $zhishiRecord;

        $timuTalk = $this->timuTalk->dataInfo(['timu_id' => $id], '*', 'id desc');
        $row['timu_talk'] = $timuTalk;

        $timuQuestions = $this->timuQuestions->dataList(['id' => $id]);
        $row['timu_questions'] = $timuQuestions;

        $this->success('查询成功', $zhishiRecord);
    }

}
